﻿package control
{
    import flash.display.GradientType;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
   
    public class Button extends Sprite
    {
        /** Dafault value of height **/
        private const _HEIGHT: Number = 22;
        /** Default value of width **/
        private const _WIDTH: Number = 100;
        /** Width **/
        private var _width: Number;
        /** Height **/
        private var _height: Number;
        /** Relative coordinate **/
        private var left: Number = undefined;
        private var right: Number = undefined;
        private var top: Number = undefined;
        private var bottom: Number = undefined;
        
        private var _text:TextField = new TextField;
        
        private var reactArea: Sprite = new Sprite;
        
        /** Rounded corner for button **/
        public var roundCorner: Number = 0;
		
		public var defaultColor: uint = 0x000000;
		
		public var link: String = "";
        
        
        /** Constructor
         * @param caption text on buttonMode
         * @param autoSize ability change widht and heithgt parameter of buttonMode
         * @param w width
         * @param h height
         */
        public function Button(caption:String = "", autoSize:Boolean=true, w:Number=_WIDTH, h:Number=_HEIGHT)
        {
            super()
            // text field
            _text.selectable = false
            _text.type = TextFieldType.DYNAMIC
            _text.autoSize = TextFieldAutoSize.LEFT
            _text.multiline = false
            _text.defaultTextFormat = new TextFormat("_sans", "12", 0x000000)
            _text.text = caption
            _text.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
            _text.x = 10
            addChild(_text)
            if (autoSize) 
            {
                _width = _text.width + 20;
                _height = _text.height;
            } 
            else 
            {
                _width = w;
                _height = h;
                _text.width = w;
                _text.height = h;
            }
            //trace("autoSize = " + autoSize + ", " + w + "x" + h);
            // add event listener
            drawUpButton();
            drawReactArea();
        }

        public function set caption (value:String): void 
        {
            _text.text = value
        }

        public function get caption (): String 
        {
            return _text.text
        }

        private function drawUpButton (): void {
            graphics.clear();
            graphics.beginFill(0x666666, 0.01);
            graphics.drawRoundRect(0, 0, _width, _height, roundCorner, roundCorner);
            graphics.endFill();
        }

        private function drawOverButton (): void {
            var matr: Matrix = new Matrix();
            matr.createGradientBox(100, 40, Math.PI/2, -25, -25);
            graphics.clear();
            graphics.beginGradientFill(GradientType.LINEAR, [0x74CCF1, 0x0B5EB0], [1.0, 1.0], [100, 255], matr);
            graphics.drawRoundRect(0, 0, _width, _height, roundCorner, roundCorner);
            graphics.endFill();
        }

        private function drawReactArea (): void {
            reactArea.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            reactArea.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            reactArea.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            reactArea.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
            
            reactArea.buttonMode = true;
            reactArea.useHandCursor = true;
            reactArea.visible = true;
            reactArea.graphics.beginFill(0xFF0000, 0.0);
            reactArea.graphics.drawRoundRect(0, 0, _width, _height, roundCorner, roundCorner);
            reactArea.graphics.endFill();
            addChild(reactArea); 
        }

        private function onMouseDown (event:MouseEvent): void {
            y += 1;
//         drawOverButton()
            filters = [new DropShadowFilter(0, 90, 0x000000, 0.7, 4, 4)];
        }
		
		public function color(color:uint):void
		{
			_text.textColor = color;
			defaultColor = color;
		}

        private function onMouseUp (event:MouseEvent): void {
            y -= 1;
            drawOverButton();
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)];        
        }

        private function onMouseOver (event:MouseEvent): void {
            drawOverButton();
            _text.textColor = 0xFFFFFF;
//         filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
        }

        private function onMouseOut (event:MouseEvent): void {
            drawUpButton();
            _text.textColor = defaultColor;
            filters = [];
        }

   }
}